/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.WarningMessage;
import me.zhengjie.modules.system.excel.listener.WarningMessageListener;
import me.zhengjie.modules.system.excel.vo.ProductVo;
import me.zhengjie.modules.system.excel.vo.WarningMessageVo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.WarningMessageRepository;
import me.zhengjie.modules.system.service.WarningMessageService;
import me.zhengjie.modules.system.service.dto.WarningMessageDto;
import me.zhengjie.modules.system.service.dto.WarningMessageQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.WarningMessageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 李忠臻
* @date 2024-02-28
**/
@Service
@RequiredArgsConstructor
public class WarningMessageServiceImpl implements WarningMessageService {

    private final WarningMessageRepository warningMessageRepository;
    private final WarningMessageMapper warningMessageMapper;

    private final WarningMessageListener warningMessageListener;
    @Override
    public PageResult<WarningMessageDto> queryAll(WarningMessageQueryCriteria criteria, Pageable pageable){
        Page<WarningMessage> page = warningMessageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(warningMessageMapper::toDto));
    }

    @Override
    public List<WarningMessageDto> queryAll(WarningMessageQueryCriteria criteria){
        return warningMessageMapper.toDto(warningMessageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public WarningMessageDto findById(Long id) {
        WarningMessage warningMessage = warningMessageRepository.findById(id).orElseGet(WarningMessage::new);
        ValidationUtil.isNull(warningMessage.getId(),"WarningMessage","id",id);
        return warningMessageMapper.toDto(warningMessage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(WarningMessage resources) {
        warningMessageRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WarningMessage resources) {
        WarningMessage warningMessage = warningMessageRepository.findById(resources.getId()).orElseGet(WarningMessage::new);
        ValidationUtil.isNull( warningMessage.getId(),"WarningMessage","id",resources.getId());
        warningMessage.copy(resources);
        warningMessageRepository.save(warningMessage);
    }

    @Override
    public void deleteAll(Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                warningMessageRepository.deleteById(id);
            }
        } else {
            warningMessageRepository.deleteAll();
        }

    }

    @Override
    public void download(List<WarningMessageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WarningMessageDto warningMessage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("条码", warningMessage.getBarCode());
//            map.put("编码", warningMessage.getCode());
            map.put("品牌", warningMessage.getProductDesc());
//            map.put("产品名称", warningMessage.getProductName());
            map.put("规格", warningMessage.getModel());
//            map.put("单位", warningMessage.getUnit());

            map.put("建议采购数", warningMessage.getProductMessage());
            map.put("即时库存量", warningMessage.getStock());
            map.put("在途数量", warningMessage.getOnPassageQuantity());
            map.put("最低库存量", warningMessage.getMiniStock());
//            map.put("创建者", warningMessage.getCreateBy());
//            map.put("更新者", warningMessage.getUpdateBy());
//            map.put("创建日期", warningMessage.getCreateTime());
//            map.put("更新时间", warningMessage.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void upload(String name, MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), WarningMessageVo.class, warningMessageListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BadRequestException(e.getMessage());
        }
    }
}